const api = require("../../../utils/marketAPIUtils");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:0,
    titles:["待支付","待发货","待收货","待评价","全部订单"],
    orderStates:[1,2,3,4,0],
    orderList:[]
  },

  _loadListData:function(){
      wx.showLoading({
        title: '加载中...',
      })
    var self = this;
    this.setData({
        orderList:[]
    });
    api.post(api.url.findMarketOrderList,{
      pageNo:1,
      state:this.data.orderStates[this.data.tabIndex]
    },function(res){
        wx.hideLoading();
        console.log(res.data);
        if(res.data.code == 200){
            const lists = res.data.body.items;
            // lists.forEach(e => {
            //    e.goodsTypeNames = e.goodsTypeNames.split(",")
            // });
            self.setData({
                orderList:lists
            })
        }
    });
  },

  //切换选项卡
  bindChange(e){
    let id = e.currentTarget.dataset.id;
    this.setData({
      state:this.data.orderStates[id],
      tabIndex:id
    })
    this._loadListData();
  },

  btnPay:function(e){
    wx.showLoading({
        title: '处理中...',
    })
    var _self = this;
    let tradeNo = e.currentTarget.dataset.id;
    let createDate = e.currentTarget.dataset.date;
    api.post(api.url.goodsPay,{
        tradeNo:tradeNo,
        createDate:createDate,
        appId:wx.getAccountInfoSync().miniProgram.appId,
        payType:3
    },function(res){
        wx.hideLoading();
        console.log(res.data);
        if(res.data.code == 200){
            var payInfo = JSON.parse(res.data.body.payInfo);
            wx.requestPayment({
                appId: payInfo.appId,
                timeStamp: payInfo.timeStamp + "",
                nonceStr: payInfo.nonceStr,
                package: payInfo.package,
                signType: payInfo.signType,
                paySign: payInfo.sign,
                success(res) {
                    console.log('支付成功');
                    _self._loadInfoData();
                }
                ,fail(res) {
                    console.log(res);
                }
            });
        }else if(res.data.msg){
            wx.showModal({
                title: '温馨提示',
                content: res.data.msg,
                showCancel: false
            })
        }else{
            wx.showToast({
                title: '支付失败',
                icon:'error'
            })
        }
    });
    },

    //关闭订单
    btnCloseOrder:function (e) {
      let tradeNo = e.currentTarget.dataset.id;
      let createDate = e.currentTarget.dataset.date;
      var self = this;
      wx.showModal({
        title: '确定要关闭订单吗？请问什么原因',
        editable:true,
        complete: (res) => {
          if (res.confirm) {
            if(!res.content){
                wx.showToast({
                    title: '请填写原因',
                    icon:'error'
                })
                return;
            }
            console.log(res.content);
            api.post(api.url.closeMarketOrder,{
                tradeNo:tradeNo,
                createDate:createDate,
                closeReason:res.content
            },function(res){
                console.log(res.data);
                if(res.data.code == 200){
                    self._loadListData();
                }else if(res.data.msg){
                    wx.showModal({
                      title: '温馨提示',
                      content: res.data.msg,
                      showCancel:false
                    })
                }else{
                    wx.showToast({
                        title: '关闭订单失败',
                        icon:'error'
                    })
                }
            });
          }
        }
      })
    },

    btnReceive(e){
        var self = this;
        let tradeNo = e.currentTarget.dataset.id;
        let createDate = e.currentTarget.dataset.date;
        wx.showModal({
            title: '温馨提示',
            content:'确定收货吗？',
            success:function(res){
                if (res.confirm) {
                    wx.showLoading({
                        title: '处理中...'
                    })
                    api.post(api.url.receiveMarketOrder,{
                        tradeNo:tradeNo
                        },function(res){
                            wx.hideLoading();
                            console.log(res.data);
                            if(res.data.code == 200){
                                self._loadListData(tradeNo);
                            }else if(res.data.msg){
                                wx.showModal({
                                    title: '温馨提示',
                                    content: res.data.msg,
                                    showCancel: false
                                })
                            }else{
                                wx.showToast({
                                    title: '订单收货失败',
                                    icon:'error'
                                })
                            }
                    });
                }
            }
        })
    },

  toInfo(e){
    let tradeNo = e.currentTarget.dataset.id;
    let createDate = e.currentTarget.dataset.date;
    wx.navigateTo({
      url: './info?tradeNo=' + tradeNo+'&createDate=' + createDate
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options && options.id && options.id < this.data.orderStates.length){
        this.setData({
          tabIndex:options.id
        })
      }
      this._loadListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})